<template>
	<view class="">
		<view class="dm_h">
			<view class="dm_l">
				<view class="dl_t">
					余额
				</view>
				<view class="dl_b">
					{{afterChange}}
				</view>
			</view>
			<!-- <view class="dm_r">
				储值管理 <u-icon name="arrow-right"></u-icon>
			</view> -->
		</view>
		<view class="code_list" v-if="list.length > 0">
			<view class="code_item" v-for="(item,index) in list" :key="index" >
				<view class="code_info">
					<view class="code_name">
						{{item.change_type_text}}
						<text style="color: #999;font-size: 12px;">({{item.created_at}})</text>
					</view>
					<view class="code_num">
						<block v-if="item.change_type == 1">-</block>
						<block v-else-if="item.change_type == 2">+</block>
						<block v-else-if="item.change_type == 3">+</block>
						{{item.change_number}}
					</view>
				</view>
				<view class="code_time">
					<view>{{item.sn}}</view>
				</view>
				
			</view>
		</view>
		<u-empty v-else icon="http://cdn.uviewui.com/uview/empty/coupon.png" text="暂无余额记录">
		</u-empty>
		<!-- <view class="tips">
			<view class="tips_t">
				充值须知
			</view>
			<ul>
				<li>资金将直接进入商家账户</li>
			</ul>
		</view> -->
		<u-modal :show="show" :title="title" showCancelButton @confirm="dhCode" @cancel="show=false">
			<view class="slot-content">
				<u--input
				    placeholder="请输入券码"
				    border="surround"
				    v-model="value"
				  ></u--input>
			</view>
		</u-modal>
		<view class="btn_b">
			<button @click="show=true" class="jin">兑换余额</button>
		</view>
	</view>
</template>

<script>
	import {jifenGet,jifenList,getUserInfo} from '@/api/my.js'
	export default {
		data() {
			return {
				show:false,
				title:'兑换余额',
				value:'',
				afterChange:0,
				page:1,
				list:[]
			}
		},
		// onLoad() {
		// 	this.getCodeList();
			
		// },
		onShow() {
			this.getCodeList();
		},
		onReachBottom() {
			this.onLoadMore()
		},
		methods: {
			getinfo(){
				getUserInfo({}).then(res=>{
					this.afterChange = res.data.use_points
				})
			},
			getCodeList(){
				let data = {
					page:this.page
				}
				jifenList(data).then(res=>{
					this.list = res.data.item;
					this.getinfo();
				})
			},
			onLoadMore() {
				
				this.page++
				this.getCodeList()
			},
			dhCode(){
				
				if(!this.value){
					uni.showToast({
						icon:'none',
						title:'请输入请输入券码'
					})
					return false
				}
				
				const data = {
					code:this.value
				}
				jifenGet(data).then(res=>{
					console.log(res)
					this.show = false
					uni.showToast({
						icon:'none',
						title:res.msg
					})
					this.page = 1;
					this.list = [];
					this.getCodeList();
					
				}).catch(err=>{
					console.log(err)
					uni.showToast({
						title:err.data,
						icon:'none'
					})
				})
			}
		}
	}
</script>

<style lang="scss">
	.dm_h {
		display: flex;
		justify-content: space-between;
		align-items: flex-end;
		padding: 16px 12px;
		border-bottom: 1px solid #eee;

		.dm_l {
			.dl_t {
				margin-bottom: 10px;
			}

			.dl_b {
				font-weight: 700;
				font-size: 20px;
			}
		}

		.dm_r {
			display: flex;
			align-items: center;
		}
	}
	.code_list{
		padding: 12px;
		.code_item{
			padding: 12px;
			background-color: #fff;
			// border-radius: 10px;
			display: flex;
			flex-direction: column;
			font-size: 14px;
			margin-bottom: 5px;
			border-bottom: 1px solid #eee;
			.code_info{
				display: flex;
				justify-content: space-between;
				margin-bottom: 12px;
				.code_num{
					font-weight: 700;
				}
			}
			.code_time{
				color: #999;
				font-size: 12px;
			}
		}
	}
	.tips {
		padding: 30px 12px;
		font-size: 14px;

		ul {
			padding-left: 10px;
			color: #999;
			margin-top: 10px;
			font-size: 12px;

			li:before {
				content: '';
				width: 4px;
				height: 4px;
				display: inline-block;
				border-radius: 50%;
				background-color: #999;
				vertical-align: middle;
				margin-right: 10px;
			}
		}
	}

	.btn_b {
		position: absolute;
		left: 0;
		bottom: 0;
		padding: 8px 12px;
		border-top: 1px solid #eee;
		width: 100%;
		box-sizing: border-box;
		.jin {
			background-color: #ff4444;
			width: 100%;
			height: 45px;
			border-radius: 30px;
			line-height: 45px;
			color: #fff;
		}
		.jin::after{
			border: none;
		}
	}
</style>